<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>EduVerse凭证测试</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .credential-card {
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 15px;
            transition: transform 0.2s;
        }
        .credential-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        .credential-card.expired {
            border-color: #ffc107;
            background-color: #fff8e1;
        }
        .credential-card.revoked {
            border-color: #f44336;
            background-color: #ffebee;
        }
        .credential-card.active {
            border-color: #4caf50;
            background-color: #e8f5e9;
        }
        .status-badge {
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: bold;
        }
        .status-badge.active {
            background-color: #4caf50;
            color: white;
        }
        .status-badge.expired {
            background-color: #ffc107;
            color: #333;
        }
        .status-badge.revoked {
            background-color: #f44336;
            color: white;
        }
        .loading {
            display: flex;
            justify-content: center;
            padding: 30px;
        }
    </style>
</head>
<body>
    <div class="container py-4">
        <header class="pb-3 mb-4 border-bottom">
            <h1 class="display-5 fw-bold">EduVerse凭证测试页面</h1>
            <p class="lead">这个页面显示了各种状态的凭证示例</p>
        </header>

        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">凭证筛选</h5>
                        <div class="btn-group w-100" role="group">
                            <button type="button" class="btn btn-outline-primary active" data-filter="all">所有凭证</button>
                            <button type="button" class="btn btn-outline-success" data-filter="active">有效凭证</button>
                            <button type="button" class="btn btn-outline-warning" data-filter="expired">已过期凭证</button>
                            <button type="button" class="btn btn-outline-danger" data-filter="revoked">已撤销凭证</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div id="loading" class="loading">
            <div class="spinner-border text-primary" role="status">
                <span class="visually-hidden">加载中...</span>
            </div>
        </div>

        <div id="credentials-container" class="row" style="display: none;">
            <!-- 凭证将通过JavaScript动态添加 -->
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const credentialsContainer = document.getElementById('credentials-container');
            const loadingIndicator = document.getElementById('loading');
            const filterButtons = document.querySelectorAll('[data-filter]');
            let allCredentials = [];

            // 从API获取凭证
            async function fetchCredentials() {
                try {
                    const response = await fetch('/api/wallet/credentials');
                    const data = await response.json();
                    
                    if (data.success) {
                        allCredentials = data.credentials;
                        renderCredentials(allCredentials);
                    } else {
                        showError('获取凭证失败: ' + (data.error || '未知错误'));
                    }
                } catch (error) {
                    showError('请求出错: ' + error.message);
                } finally {
                    loadingIndicator.style.display = 'none';
                    credentialsContainer.style.display = 'flex';
                }
            }

            // 显示错误信息
            function showError(message) {
                loadingIndicator.style.display = 'none';
                credentialsContainer.innerHTML = `
                    <div class="col-12">
                        <div class="alert alert-danger">${message}</div>
                    </div>
                `;
                credentialsContainer.style.display = 'flex';
            }

            // 计算凭证状态
            function getCredentialStatus(credential) {
                const now = new Date();
                if (credential.revoked) {
                    return 'revoked';
                } else if (credential.expirationDate && new Date(credential.expirationDate) < now) {
                    return 'expired';
                } else {
                    return 'active';
                }
            }

            // 获取显示名称
            function getTypeName(type) {
                if (Array.isArray(type)) {
                    const mainType = type[type.length - 1];
                    
                    switch(mainType) {
                        case 'academic-degree':
                            return '学术学位';
                        case 'professional-certification':
                            return '专业认证';
                        case 'skill-verification':
                            return '技能验证';
                        case 'kyc-age':
                            return '年龄验证';
                        default:
                            return mainType;
                    }
                }
                return '未知类型';
            }

            // 渲染凭证卡片
            function renderCredentials(credentials) {
                if (credentials.length === 0) {
                    credentialsContainer.innerHTML = `
                        <div class="col-12">
                            <div class="alert alert-info">没有找到符合条件的凭证</div>
                        </div>
                    `;
                    return;
                }

                credentialsContainer.innerHTML = '';
                
                credentials.forEach(credential => {
                    const status = getCredentialStatus(credential);
                    const typeName = getTypeName(credential.type);
                    
                    const cardHTML = `
                        <div class="col-md-6 col-lg-4">
                            <div class="credential-card ${status}">
                                <div class="d-flex justify-content-between align-items-start mb-3">
                                    <h5 class="card-title">${typeName}</h5>
                                    <span class="status-badge ${status}">
                                        ${status === 'active' ? '有效' : status === 'expired' ? '已过期' : '已撤销'}
                                    </span>
                                </div>
                                
                                <div class="mb-3">
                                    <small class="text-muted">凭证ID:</small>
                                    <p class="mb-0 text-truncate" title="${credential.id}">${credential.id}</p>
                                </div>
                                
                                <div class="mb-3">
                                    <small class="text-muted">发行方:</small>
                                    <p class="mb-0 text-truncate" title="${credential.issuer}">${credential.issuer}</p>
                                </div>
                                
                                <div class="row mb-3">
                                    <div class="col-6">
                                        <small class="text-muted">发行日期:</small>
                                        <p class="mb-0">
                                            ${new Date(credential.issuanceDate).toLocaleDateString()}
                                        </p>
                                    </div>
                                    <div class="col-6">
                                        <small class="text-muted">过期日期:</small>
                                        <p class="mb-0">
                                            ${credential.expirationDate 
                                                ? new Date(credential.expirationDate).toLocaleDateString() 
                                                : '永不过期'}
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    `;
                    
                    credentialsContainer.innerHTML += cardHTML;
                });
            }

            // 筛选凭证
            function filterCredentials(filter) {
                if (filter === 'all') {
                    renderCredentials(allCredentials);
                    return;
                }
                
                const filtered = allCredentials.filter(credential => {
                    return getCredentialStatus(credential) === filter;
                });
                
                renderCredentials(filtered);
            }

            // 设置筛选按钮事件
            filterButtons.forEach(button => {
                button.addEventListener('click', function() {
                    // 移除所有按钮的active类
                    filterButtons.forEach(btn => {
                        btn.classList.remove('active');
                    });
                    
                    // 添加当前按钮的active类
                    this.classList.add('active');
                    
                    // 筛选凭证
                    filterCredentials(this.getAttribute('data-filter'));
                });
            });

            // 获取凭证
            fetchCredentials();
        });
    </script>
</body>
</html> 